//
//  RiddingAppDelegate.m
//  Ridding
//
//  Created by zys on 12-3-19.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "RiddingAppDelegate.h"
#import "RiddingViewController.h"
#import "TutorialViewController.h"
#import "HomeViewController.h"
#import "InvitationViewController.h"
#import "LandingViewController.h"
#import "StaticInfo.h"
#import "UserSettingViewController.h"


@implementation RiddingAppDelegate

@synthesize window = _window;
@synthesize rootViewController = _rootViewController;
@synthesize navController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [MobClick checkUpdate];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
    [MobClick startWithAppkey:YouMenAppKey reportPolicy:REALTIME channelId:nil];
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   
    navController = [[UINavigationController alloc]init];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    //第一次登陆
    BOOL isSecondTimeLaunched = [prefs boolForKey:@"isSecondTimeLaunched"];
    
    if (!isSecondTimeLaunched) {
        LandingViewController *landingViewController = [[LandingViewController alloc] init];
        [navController pushViewController:landingViewController animated:YES];
        [prefs setBool:YES forKey:@"isSecondTimeLaunched"];
    }
    else { 
        self.rootViewController = [[RiddingViewController alloc]init];
        [navController pushViewController:self.rootViewController animated:YES];
    }
    [navController setNavigationBarHidden:YES];
    
    self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];
   
    //call the Appirater class
    //[Appirater appLaunched];
    
    return YES;
}

-(bool)canLogin{
    StaticInfo* staticInfo=[StaticInfo getSinglton];
    RequestUtil* requestUtil=[RequestUtil getSinglton];
    SinaApiRequestUtil* request=[SinaApiRequestUtil getSinglton];
    NSDictionary *userProfileDic = [requestUtil getUserProfile];
    //如果新浪成功，并且authtoken有效
    if([request checkTokenIsValid]&&userProfileDic!=nil){
        staticInfo.user.name=[userProfileDic objectForKey:@"nickname"];
        staticInfo.user.bavatorUrl=[userProfileDic objectForKey:@"bavatorurl"];
        staticInfo.user.savatorUrl=[userProfileDic objectForKey:@"savatorurl"];
        staticInfo.user.totalDistance=[userProfileDic objectForKey:@"totalDistance"];
        staticInfo.user.nowRiddingCount=[[userProfileDic objectForKey:@"riddingCount"]intValue];
        return true;
    }else{
        [SFHFKeychainUtils deleteItemForUsername:staticInfo.user.userId andServiceName:@"riddingapp" error:nil];
    }
    return false;
}


//iPhone 从APNs服务器获取deviceToken后回调此方法
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString* dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    NSLog(@"deviceToken:%@", dt);
}

//注册push功能失败 后 返回错误信息，执行相应的处理
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
    NSLog(@"Push Register Error:%@", err.description);
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

@end
